package com.dsz.usercenter.controller;

import com.dsz.usercenter.common.BaseResponse;
import com.dsz.usercenter.common.ErrorCode;
import com.dsz.usercenter.common.ResultUtil;
import com.dsz.usercenter.exception.BusinessException;
import com.dsz.usercenter.model.domain.Team;
import com.dsz.usercenter.model.domain.User;
import com.dsz.usercenter.model.request.*;
import com.dsz.usercenter.model.vo.TeamUserVO;
import com.dsz.usercenter.service.TeamService;
import com.dsz.usercenter.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/team")
@CrossOrigin(origins = "http://127.0.0.1:5173", allowCredentials = "true")
public class TeamController {

    @Resource
    TeamService teamService;
    @Resource
    UserService userService;

    /**
     * 获取用户加入的队伍
     * @param teamQueryRequest 队伍请求类
     * @return 队伍列表
     */
    @GetMapping("/my/join")
    public BaseResponse<List<TeamUserVO>> queryJoinTeam(TeamQueryRequest teamQueryRequest, HttpServletRequest request) {
        if (teamQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User currentUser = userService.getCurrentUser(request);
        List<TeamUserVO> userVOList = teamService.listJoinTeam(teamQueryRequest,currentUser);
        return ResultUtil.ok(userVOList);
    }

    /**
     * 获取用户创建的队伍
     * @param teamQueryRequest 队伍请求类
     * @return 队伍列表
     */
    @GetMapping("/my/create")
    public BaseResponse<List<TeamUserVO>> queryCreateTeam(TeamQueryRequest teamQueryRequest) {
        if (teamQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<TeamUserVO> userVOList = teamService.listTeam(teamQueryRequest, true);
        return ResultUtil.ok(userVOList);
    }

    /**
     * 用户退出队伍
     * @param teamQuitRequest 退出队伍请求类
     * @param request 请求
     * @return 是否成功
     */
    @GetMapping("/quit")
    public BaseResponse<Boolean> quitTeam(TeamQuitRequest teamQuitRequest, HttpServletRequest request) {
        if (teamQuitRequest == null || request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User currentUser = userService.getCurrentUser(request);
        boolean res = teamService.quitTeam(teamQuitRequest, currentUser);
        return ResultUtil.ok(res);
    }

    /**
     * 用户加入队伍
     * @param teamJoinRequest 队伍加入参数
     * @param request 请求
     * @return 是否加入成功
     */
    @PostMapping("/join")
    public BaseResponse<Boolean> joinTeam(@RequestBody TeamJoinRequest teamJoinRequest, HttpServletRequest request) {
        if (teamJoinRequest == null || request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User currentUser = userService.getCurrentUser(request);
        boolean res = teamService.joinTeam(teamJoinRequest, currentUser);
        return ResultUtil.ok(res);
    }

    /**
     * 创建队伍
     * @param teamAddRequest 队伍
     * @param request 请求
     * @return 队伍id
     */
    @PostMapping("/add")
    public BaseResponse<Long> addTeam(@RequestBody TeamAddRequest teamAddRequest, HttpServletRequest request) {
        if (teamAddRequest == null || request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long res = teamService.saveTeam(teamAddRequest, request);
        return ResultUtil.ok(res);
    }

    /**
     * 删除队伍
     * @param id 队伍id
     * @return 是否删除成功
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteTeam(Long id, HttpServletRequest request) {
        if (id == null || id <= 0 || request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User currentUser = userService.getCurrentUser(request);
        boolean res = teamService.removeTeamById(id, currentUser);
        return ResultUtil.ok(res);
    }

    /**
     * 更新队伍
     * @param teamUpdateRequest 队伍
     * @return 是否更新成功
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> modifyTeam(@RequestBody TeamUpdateRequest teamUpdateRequest, HttpServletRequest request) {
        if (teamUpdateRequest == null || request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User currentUser = userService.getCurrentUser(request);
        boolean res = teamService.updateTeam(teamUpdateRequest, currentUser);
        return ResultUtil.ok(res);
    }

    /**
     * 根据id查找队伍
     * @param id 队伍id
     * @return 队伍
     */
    @GetMapping("/get")
    public BaseResponse<Team> queryById(Long id) {
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Team team = teamService.getTeamById(id);
        return ResultUtil.ok(team);
    }

    /**
     * 获取队伍列表
     * @param teamQueryRequest 队伍请求参数
     * @return 队伍列表
     */
    @GetMapping("/list")
    public BaseResponse<List<TeamUserVO>> query(TeamQueryRequest teamQueryRequest, HttpServletRequest request) {
        if (teamQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User currentUser = userService.getCurrentUser(request);
        boolean isAdmin = userService.isAdmin(currentUser);
        List<TeamUserVO> teamList = teamService.listTeam(teamQueryRequest, isAdmin);
        return ResultUtil.ok(teamList);
    }

    /**
     * 分页查询
     * @param teamQueryRequest 队伍请求参数
     * @return 队伍列表
     */
    @GetMapping("/page")
    public BaseResponse<List<Team>> pageQuery(TeamQueryRequest teamQueryRequest) {
        if (teamQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<Team> teamList = teamService.pageTeam(teamQueryRequest);
        return ResultUtil.ok(teamList);
    }

}
